﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using personalplaner.core.sql;
using personalplaner.business.common;
using System.Data.Odbc;
using System.Windows;

namespace personalplaner.databasewizard.services
{
    public interface ICreateDatabaseService : IDatabaseService
    {
    }

    public class CreateDatabaseService : DatabaseServiceBase, ICreateDatabaseService
    {
        public CreateDatabaseService()
            : base("root/new/file", "db.gc")
        {
        }

        public override void Execute(string odbcSource, string user, string password, string database)
        {
            string connectionStr = string.Format("DSN={0};UID={1};PWD={2};Database=master;", odbcSource, user, password);
            var settings = new DatabaseSettings
            {
                Connection = new OdbcConnection(connectionStr)
            };

            string sqlCreateDBQuery = string.Format("CREATE DATABASE {0}", database);
            //+ " ON PRIMARY "
            //+ " (NAME = " + DBParam.DataFileName + ", "
            //+ " FILENAME = '" + DBParam.DataPathName + "', "
            //+ " SIZE = 2MB,"
            //+ " FILEGROWTH =" + DBParam.DataFileGrowth + ") "
            //+ " LOG ON (NAME =" + DBParam.LogFileName + ", "
            //+ " FILENAME = '" + DBParam.LogPathName + "', "
            //+ " SIZE = 1MB, "
            //+ " FILEGROWTH =" + DBParam.LogFileGrowth + ") ";

            IsNoError = SimpleQuerry.Execute(settings, sqlCreateDBQuery, true);

            if (IsNoError)
            {
                connectionStr = string.Format("DSN={0};UID={1};PWD={2};Database={3};", odbcSource, user, password, database);
                Settings = new DatabaseSettings
                {
                    Connection = new OdbcConnection(connectionStr)
                };

                IsNoError = SimpleQuerry.Execute(Settings, string.Format("exec('ALTER DATABASE {0} SET RECOVERY SIMPLE')", database), true);
                IsNoError = SimpleQuerry.Execute(Settings, string.Format("exec('ALTER DATABASE {0} set auto_close off')", database), true);

                bool tmp = true;
                int index = 0;
                while (tmp)
                {
                    tmp = ExecuteSqlFiles(index);
                    index++;

                    if (!IsNoError)
                    {
                        tmp = false;
                    }
                }

                Mediator.NotifyColleagues<string>("AppendOutputString", string.Format("End of creating new Database {0}", database));
                //ExecuteFinishEvent();
                Mediator.NotifyColleagues("DatabaseExetueFinished");
            }
            else
            {
                MessageBox.Show("Eine Datenbank mit diesem Namen existiert bereits.\nBitte wählen Sie einen anderen Datenbanknamen aus.", "Fehler", MessageBoxButton.OK);
                //ExecuteErrorEvent("Eine Datenbank mit diesem Namen existiert bereits.\nBitte wählen Sie einen anderen Datenbanknamen aus.", "Fehler");
                //if (OnReload != null)
                //    OnReload();
            }
        }
    }
}
